﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Pixiv.PixivArt
{
    internal class ArtHelper
    {
        /// <summary>
        /// 获取作者ID
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public int GetArtId(string Url)
        {
            if (TextHelper.IsIntNumber(Url))
                return Convert.ToInt32(Url);
            String GetCount = TextHelper.RegexText(Url, "id=[0-9]{1,30}").Replace("id=", "");
            if (TextHelper.IsIntNumber(GetCount))
                return Convert.ToInt32(GetCount);
            else
                return 0;
        }

        /// <summary>
        /// 批处理模式
        /// </summary>
        /// <param name="FlodePath"></param>
        /// <param name="ScoreText"></param>
        public void InspectFloder(string FlodePath, string ScoreText)
        {
            List<string> FileList = FloderNameHelper.GetFullFileNameList(FlodePath);
            int CompltedCount = 0;
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, CompltedCount++);
                InspectFile(SingleFile, ScoreText);
            }
        }

        /// <summary>
        /// 检查单个图片列表是否符合规定
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="ScoreText"></param>
        public void InspectFile(string FilePath, string ScoreText)
        {
            int Score = TextHelper.StringToInt(ScoreText);
            List<string> ImgList = FileHelper.ReadAllLine(FilePath);
            int CompltedCount = 0;
            List<string> TempList = new List<string>();
            foreach (String SingleImg in ImgList)
            {
                new PrintLog().PercentLog(ImgList.Count, CompltedCount++);
                int ImgId = new PixivHelper().GetImgId(SingleImg);
                Console.WriteLine("图像ID{0}链接{1}", ImgId, SingleImg);
                if (ImgId == 0)
                    continue;

                int ImgScore = new PixivHelper().GetScore(ImgId);
                Console.WriteLine("评分{0}", ImgScore);
                if (ImgScore >= Score)
                    TempList.Add(SingleImg);
            }
            FileHelper.WriteList(FileNameHelper.CreateFileNameAddTip(FilePath, string.Format("_评分高于{0}", Score)), TempList);
        }
    }
}